package com.itheima.test;

import com.itheima.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();
        lo:
        while (true) {
            System.out.println("--------欢迎来到英雄管理系统--------");
            System.out.println("1 添加英雄");
            System.out.println("2 删除英雄");
            System.out.println("3 修改英雄");
            System.out.println("4 查看英雄");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addStudent(list);
                    break;
                case "2":
                    //System.out.println("删除英雄");
                    deleteStudent(list);
                    break;
                case "3":
                   // System.out.println("修改英雄");
                    updataStudent(list);
                    break;
                case "4":
                    // System.out.println("查看英雄");
                    queryStudents(list);
                    break;
                case "5":
                    System.out.println("谢谢你的使用");
                    break lo;
                default:
                    System.out.println("你的输入有误");
            }
        }

    }

    public static void updataStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要修改的英雄学号");
        String updatasid = sc.next();
        //调用方法去获取索引
        int index = getIndex(list,updatasid);
        if(index == -1){
            System.out.println("查无此人,请重试");
        }else {
            System.out.println("请输入新的姓名:");
            String name = sc.next();
            System.out.println("请输入新的年龄:");
            int age = sc.nextInt();
            System.out.println("请输入新的生日:");
            String birthday = sc.next();
            Student stu = new Student(updatasid, name, age, birthday);
            list.set(index,stu);
            System.out.println("修改成功");
        }
    }

    public static void deleteStudent(ArrayList<Student> list) {
        //注意 sc是一个对象 而对象类似变量 只在作用域中有效
        //这也是为什么要将list带人进来
        //也侧面体现出 方法调用虽然在main中 但调只是方法的返回值
        //即除了形参 都与main无关
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要删除的英雄学号");
        String deletesid = sc.next();
        //调用方法去获取索引
        int index = getIndex(list,deletesid);
        if (index == -1){
            System.out.println("查无此人,请重新尝试");
        }else {
            list.remove(index);
            System.out.println("删除成功!");
        }
    }

    public static void queryStudents(ArrayList<Student> list) {
        //1.判断是否有数据
        if (list.size() == 0) {
            System.out.println("无信息,请添加后查询");
            return;
        }
        //2,存在 表头数据
        System.out.println("学号\t\t姓名\t年龄\t生日");
        //3.遍历
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
        }
    }

    public static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        //先定义 再套用无限循环 避免学号丢失
        String sid;
        while (true){
            System.out.println("请输入学号:");
            sid = sc.next();
            int index = getIndex(list,sid);
            if (index == -1){
                break;
            }else {
                System.out.println("当前学号已被占用,请重新输入 ");
            }
        }
        System.out.println("请输入姓名:");
        String name = sc.next();
        System.out.println("请输入年龄:");
        int age = sc.nextInt();
        System.out.println("请输入生日:");
        String birthday = sc.next();

        Student stu = new Student(sid, name, age, birthday);
        list.add(stu);
        System.out.println("添加成功");
    }

    //因为修改和删除需要索引 定义一个方法取索引
    public static int getIndex(ArrayList<Student> list, String sid) {
        //标记位思想
        //1假设不存在
        int index = -1;
        //2遍历集合,获取每一个学生对象,准备进行查找
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            //获取每一个学生的学号
            String id = stu.getSid();
            //将每一个学生的学号于传的id比对 存在修改标记
            if (id.equals(sid)) {
                index = i;
            }
        }
        return index;
    }
}
